package com.zhongwang.qctApp.view

import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.annotation.Nullable
import com.google.zxing.integration.android.IntentIntegrator
import com.zhongwang.qctApp.R
import com.zhongwang.qctApp.utils.SysConfig

/**
 * 扫描跳转&&扫描结果接收界面
 * @author 徐一贺
 */
class ScanActivity : AppCompatActivity() {
    private val scanContent = this
    private var purpose: String? = null
    private var account: String = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_scan)
        val intentIntegrator = IntentIntegrator(this)
        //启动自定义的扫描活动，不设置则启动默认的活动
        intentIntegrator.captureActivity = ScanCodeActivity::class.java
        //启动扫描
        intentIntegrator.initiateScan()
        //接收传值
        val bundle: Bundle? = intent.extras
        if (bundle != null) {
            purpose = when (bundle.getString("purpose").toString()) {
                "applyCheck" -> "1"
                "receive" -> "3"
                "check" -> "2"
                "warehousingCheck" -> "4"
                else -> "异常"
            }
            account = bundle.getString("account").toString()
        }
    }

    /**
     * 处理扫码后返回的结果
     */
    override fun onActivityResult(requestCode: Int, resultCode: Int, @Nullable data: Intent?) {
        val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
        if (result != null) {
            //==是否扫到内容
            if (result.contents != null) {
                //替换歧义字符
                //替换英文反斜杠为◐
                var code: String = result.contents
                if (code.contains("|")) {
                    var splitCharCount = 0
                    for (charS in code.toCharArray()) {
                        if (charS == '|') {
                            splitCharCount++
                        }
                    }
                    if (splitCharCount != 3) {
                        Toast.makeText(this, "因二维码格式错误，二维码识别失败,请重新扫码。", Toast.LENGTH_LONG).show()
                        val webViewControlIntent =
                            Intent(scanContent, WebViewControlActivity::class.java)
                        webViewControlIntent.putExtra(
                            "url",
                            SysConfig.SYSTEM_NET_URL + "index"
                        )
                        startActivity(webViewControlIntent)
                    }
                    //获取码中数据的列表
                    val codeArr: List<String> = code.split("|")
                    if (codeArr[0] == null || codeArr[1] == null || codeArr[2] == null || codeArr[3] == null){
                        Toast.makeText(this, "因二维码格式错误，二维码识别失败,请重新扫码。", Toast.LENGTH_LONG).show()
                        val webViewControlIntent =
                            Intent(scanContent, WebViewControlActivity::class.java)
                        webViewControlIntent.putExtra(
                            "url",
                            SysConfig.SYSTEM_NET_URL + "index"
                        )
                        startActivity(webViewControlIntent)
                    }else{
                        //通过截取最后分隔符后的字符获取跳转方向生产转序还是装配转序
                        code.substring(code.lastIndexOf('|') + 1)
                        //获取码中数据的列表
                        val codeArr: List<String> = code.split("|")
                        //获取部分主键
                        val partId: String = codeArr[2]
                        //获取类型数
                        val typeNum: String = codeArr[3]
                        for (i in partId.indices) {
                            val partIdChar: Char = partId[i]
                            if (partIdChar != '0' && partIdChar != '1' && partIdChar != '2' && partIdChar != '3' && partIdChar != '4' && partIdChar != '5' && partIdChar != '6' && partIdChar != '7' && partIdChar != '8' && partIdChar != '9') {
                                Toast.makeText(this, "因二维码格式错误，二维码识别失败,请重新扫码。", Toast.LENGTH_LONG).show()
                                val webViewControlIntent =
                                    Intent(scanContent, WebViewControlActivity::class.java)
                                webViewControlIntent.putExtra(
                                    "url",
                                    SysConfig.SYSTEM_NET_URL + "index"
                                )
                                startActivity(webViewControlIntent)
                                break
                            }
                        }
                        for (i in typeNum.indices) {
                            val typeChar: Char = typeNum[i]
                            if (typeChar != '1' && typeChar != '2' && typeChar != '3') {
                                Toast.makeText(this, "因二维码格式错误，二维码识别失败,请重新扫码。", Toast.LENGTH_LONG).show()
                                val webViewControlIntent =
                                    Intent(scanContent, WebViewControlActivity::class.java)
                                webViewControlIntent.putExtra(
                                    "url",
                                    SysConfig.SYSTEM_NET_URL + "index"
                                )
                                startActivity(webViewControlIntent)
                                break
                            }
                        }
                        val webViewControlIntent =
                            Intent(scanContent, WebViewControlActivity::class.java)
                        code = Uri.encode(code)
                        webViewControlIntent.putExtra(
                            "url",
                            SysConfig.SYSTEM_NET_URL + "result"
                        )
                        webViewControlIntent.putExtra(
                            "type",
                            purpose
                        )
                        webViewControlIntent.putExtra(
                            "code",
                            code
                        )
                        startActivity(webViewControlIntent)
                    }
                } else {
                    Toast.makeText(this, "因二维码格式错误，二维码识别失败,请重新扫码。", Toast.LENGTH_LONG).show()
                    val webViewControlIntent =
                        Intent(scanContent, WebViewControlActivity::class.java)
                    webViewControlIntent.putExtra(
                        "url",
                        SysConfig.SYSTEM_NET_URL + "index"
                    )
                    startActivity(webViewControlIntent)
                }
            } else {
                val webViewControlIntent =
                    Intent(scanContent, WebViewControlActivity::class.java)
                webViewControlIntent.putExtra(
                    "url",
                    SysConfig.SYSTEM_NET_URL + "index"
                )
                startActivity(webViewControlIntent)
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data)
        }
    }
}